﻿package {
	import flash.display.Sprite;
		import flash.events.Event;
		import flash.display.StageAlign;
		import flash.display.StageScaleMode;
			[SWF(width="836", height="464")];
			
			
			public class Planets extends Sprite {
				
				public var center_Y:Number;
				public var center_X:Number;
				private var scale:Number;
				private var Planetscale:Number;
				private var maxZoom: Number;
				private var Zoomm:Number = 1;
				private var Time:Number = 0;
				private var Timescale:Number;
				private var delta_t: Number = 5;
				private var PlanetR:Array = new Array(10);
				private var PlanetOrbitR:Array = new Array(10);
				private var PlanetPeriod:Array = new Array(10);
				private var PlanetAngle:Array = new Array(10);
				private var PlanetColor:Array = new Array(10);
				private var PlanetName:Array = new Array(10);
				
				private var Sun:Sphere;
				private var Mercury:Sphere;
				private var Venus:Sphere;
				private var Earth:Sphere;
				private var Mars:Sphere;
				private var Jupiter:Sphere;
				private var Saturn:Sphere;
				private var Uranus:Sphere;
				private var Neptune:Sphere;
				private var Pluto:Sphere;
				
				private var controlpanel:ControlPanel;
				private var Zoomslider:HorizontalSlider;
				private var Timeslider:HorizontalSlider;
				
				
				public function Planets(center_Y:Number=200, center_X:Number=300) {
					
					this.center_Y = center_Y;
					this.center_X = center_X;
					

					init();
		}
		public function init():void {
			
			PlanetR = [695000, 2440, 6051, 6378, 3397, 71492, 60268, 25559, 24766, 2274];    //km
			PlanetOrbitR = [0, 57910, 108200, 149600, 227940, 778330, 1429400, 2870990, 4504300, 5913520]; // km /1000
			PlanetAngle = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
			PlanetPeriod = [0, 88, 225, 365, 687, 4333, 10760, 30685, 60190, 90550]; //days
			PlanetName = ["Sun", "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"];
			//PlanetColor = [0xff0000, 0xff0000, 0xff0000, 0xff0000, 0xff0000, 0xff0000, 0xff0000, 0xff0000, 0xff0000, 0xff0000];
			maxZoom = 5;
			Zoomm = 1;
			Planetscale = 2000;
			scale = 5000;
			delta_t = 1;
			
			controlpanel = new ControlPanel(stage.stageHeight, stage.stageWidth);
			addChild(controlpanel);
			
			Zoomslider = new HorizontalSlider(0.1, maxZoom, 1, 0.1, false);
			Zoomslider.x = .65*stage.stageWidth;
			Zoomslider.y = .91*stage.stageHeight;
			Zoomslider.height=.077*stage.stageHeight;
			addChild(Zoomslider);
			
			Timeslider = new HorizontalSlider(1, 100, 1, 1, false);
			Timeslider.x = .3*stage.stageWidth;
			Timeslider.y = .91*stage.stageHeight;
			Timeslider.height=.077*stage.stageHeight;
			addChild(Timeslider);
						
			Sun = new Sphere(PlanetR[0]/(50*Planetscale), 0xffff00, 0x999900);
			addChild(Sun);
			Sun.x = center_X;
			Sun.y = center_Y;
			
			Mercury = new Sphere(PlanetR[1]/Planetscale, 0xff0000, 0x000000);
			addChild(Mercury);
			Mercury.x = center_X + Math.cos(PlanetAngle[1]) * (PlanetOrbitR[1]/scale);
			Mercury.y = center_Y - Math.sin(PlanetAngle[1]) * (PlanetOrbitR[1]/scale);
			
			Venus = new Sphere(PlanetR[2]/Planetscale, 0x006600, 0x000000);
			addChild(Venus);
			Venus.x = center_X + Math.cos(PlanetAngle[2]) * (PlanetOrbitR[2]/scale);
			Venus.y = center_Y - Math.sin(PlanetAngle[2]) * (PlanetOrbitR[2]/scale);
			
			Earth = new Sphere(PlanetR[3]/Planetscale, 0x3366ff, 0x000000);
			addChild(Earth);
			Earth.x = center_X + Math.cos(PlanetAngle[3]) * (PlanetOrbitR[3]/scale);
			Earth.y = center_Y - Math.sin(PlanetAngle[3]) * (PlanetOrbitR[3]/scale);
			
			Mars = new Sphere(PlanetR[4]/Planetscale, 0xcc0000, 0x000000);
			addChild(Mars);
			Mars.x = center_X + Math.cos(PlanetAngle[4]) * (PlanetOrbitR[4]/scale);
			Mars.y = center_Y - Math.sin(PlanetAngle[4]) * (PlanetOrbitR[4]/scale);
			
			Jupiter = new Sphere(PlanetR[5]*0.8/Planetscale, 0xff6600, 0xcc0000);
			addChild(Jupiter);
			Jupiter.x = center_X + Math.cos(PlanetAngle[5]) * (PlanetOrbitR[5]/scale);
			Jupiter.y = center_Y - Math.sin(PlanetAngle[5]) * (PlanetOrbitR[5]/scale);
			
			Saturn = new Sphere(PlanetR[6]*0.8/Planetscale, 0x006699, 0x000000);
			addChild(Saturn);
			Saturn.x = center_X + Math.cos(PlanetAngle[6]) * (PlanetOrbitR[6]/scale);
			Saturn.y = center_Y - Math.sin(PlanetAngle[6]) * (PlanetOrbitR[6]/scale);
			
			Uranus = new Sphere(PlanetR[7]/Planetscale, 0x990066, 0x000000);
			addChild(Uranus);
			Uranus.x = center_X + Math.cos(PlanetAngle[7]) * (PlanetOrbitR[7]/scale);
			Uranus.y = center_Y - Math.sin(PlanetAngle[7]) * (PlanetOrbitR[7]/scale);
			
			Neptune = new Sphere(PlanetR[8]/Planetscale, 0x000066, 0x000000);
			addChild(Neptune);
			Neptune.x = center_X + Math.cos(PlanetAngle[8]) * (PlanetOrbitR[8]/scale);
			Neptune.y = center_Y - Math.sin(PlanetAngle[8]) * (PlanetOrbitR[8]/scale);
			
			Pluto = new Sphere(PlanetR[9]*2/Planetscale, 0x660000, 0x000000);
			addChild(Pluto);
			Pluto.x = center_X + Math.cos(PlanetAngle[9]) * (PlanetOrbitR[9]/scale);
			Pluto.y = center_Y - Math.sin(PlanetAngle[9]) * (PlanetOrbitR[9]/scale);
			
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
			
		}
		
			public function onEnterFrame(event:Event):void {
			
			   Zoomm = Zoomslider.value;
			   Timescale = Timeslider.value;
			
				if(!controlpanel.pause) {Time += delta_t*Timescale;}
				if(controlpanel.stepback) {Time -= delta_t/2*Timescale;}
				if(controlpanel.stepforward) {Time += delta_t/2*Timescale;}
				if(controlpanel.reset) {
					Time = 0; 
					controlpanel.pause = true;
				}

				graphics.clear();
				drawPlanets();

				
			}
			private function drawPlanets():void {
				
				Sun.x = center_X;
			    Sun.y = center_Y;
				Sun.scaleX = Zoomm;
				Sun.scaleY = Zoomm;
				
				Mercury.x = center_X + Math.cos(PlanetAngle[1]+ (Time/PlanetPeriod[1])) * (PlanetOrbitR[1]*Zoomm/scale);
				Mercury.y = center_Y - Math.sin(PlanetAngle[1]+ (Time/PlanetPeriod[1])) * (PlanetOrbitR[1]*Zoomm/scale);
				Mercury.scaleX = Zoomm;
				Mercury.scaleY = Zoomm;
				Venus.x = center_X + Math.cos(PlanetAngle[2]+ (Time/PlanetPeriod[2])) * (PlanetOrbitR[2]*Zoomm/scale);
				Venus.y = center_Y - Math.sin(PlanetAngle[2]+ (Time/PlanetPeriod[2])) * (PlanetOrbitR[2]*Zoomm/scale);
				Venus.scaleX = Zoomm;
				Venus.scaleY = Zoomm;
				Earth.x = center_X + Math.cos(PlanetAngle[3]+ (Time/PlanetPeriod[3])) * (PlanetOrbitR[3]*Zoomm/scale);
				Earth.y = center_Y - Math.sin(PlanetAngle[3]+ (Time/PlanetPeriod[3])) * (PlanetOrbitR[3]*Zoomm/scale);
				Earth.scaleX = Zoomm;
				Earth.scaleY = Zoomm;
				Mars.x = center_X + Math.cos(PlanetAngle[4]+ (Time/PlanetPeriod[4])) * (PlanetOrbitR[4]*Zoomm/scale);
				Mars.y = center_Y - Math.sin(PlanetAngle[4]+ (Time/PlanetPeriod[4])) * (PlanetOrbitR[4]*Zoomm/scale);
				Mars.scaleX = Zoomm;
				Mars.scaleY = Zoomm;
				Jupiter.x = center_X + Math.cos(PlanetAngle[5]+ (Time/PlanetPeriod[5])) * (PlanetOrbitR[5]*Zoomm/scale);
				Jupiter.y = center_Y - Math.sin(PlanetAngle[5]+ (Time/PlanetPeriod[5])) * (PlanetOrbitR[5]*Zoomm/scale);
				Jupiter.scaleX = Zoomm; 
				Jupiter.scaleY = Zoomm;
				Saturn.x = center_X + Math.cos(PlanetAngle[6]+ (Time/PlanetPeriod[6])) * (PlanetOrbitR[6]*Zoomm/scale);
				Saturn.y = center_Y - Math.sin(PlanetAngle[6]+ (Time/PlanetPeriod[6])) * (PlanetOrbitR[6]*Zoomm/scale);
				Saturn.scaleX = Zoomm;
				Saturn.scaleY = Zoomm;
				Uranus.x = center_X + Math.cos(PlanetAngle[7]+ (Time/PlanetPeriod[7])) * (PlanetOrbitR[7]*Zoomm/scale);
				Uranus.y = center_Y - Math.sin(PlanetAngle[7]+ (Time/PlanetPeriod[7])) * (PlanetOrbitR[7]*Zoomm/scale);
				Uranus.scaleX = Zoomm;
				Uranus.scaleY = Zoomm;
				Neptune.x = center_X + Math.cos(PlanetAngle[8]+ (Time/PlanetPeriod[8])) * (PlanetOrbitR[8]*Zoomm/scale);
				Neptune.y = center_Y - Math.sin(PlanetAngle[8]+ (Time/PlanetPeriod[8])) * (PlanetOrbitR[8]*Zoomm/scale);
				Neptune.scaleX = Zoomm;
				Neptune.scaleY = Zoomm;
				Pluto.x = center_X + Math.cos(PlanetAngle[9]+ (Time/PlanetPeriod[9])) * (PlanetOrbitR[9]*Zoomm/scale);
				Pluto.y = center_Y - Math.sin(PlanetAngle[9]+ (Time/PlanetPeriod[9])) * (PlanetOrbitR[9]*Zoomm/scale);
				Pluto.scaleX = Zoomm;
				Pluto.scaleY = Zoomm;
			
			}
			
		}

}
		
		